package com.ybkj.o2o.zhuanxian.controller.res;

import com.google.common.base.Preconditions;

import com.ybkj.o2o.zhuanxian.common.DataGrid;
import com.ybkj.o2o.zhuanxian.common.error.ErrorConstantV4;
import com.ybkj.o2o.zhuanxian.common.error.ResultTransport;
import com.ybkj.o2o.zhuanxian.entity.Order;
import com.ybkj.o2o.zhuanxian.entity.OrderReview;
import com.ybkj.o2o.zhuanxian.manager.OrderManager;
import com.ybkj.o2o.zhuanxian.manager.OrderReviewManager;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("api/orderReview")
public class OrderReviewController {

  @Autowired
  private OrderReviewManager orderReviewManager;
  @Autowired
  private OrderManager orderManager;

  /**
   * 创建订单评价 .
   * 
   * @param orderId     订单Id
   * @param content     评价内容
   * @param score       评分
   * @param appKey      区别系统
   * 
   */
  @RequestMapping(value = "review", method = RequestMethod.POST)
  @ResponseBody
  public ResultTransport create(
      @RequestParam("orderId") Long orderId,
      @RequestParam(required = false, value = "content") String content,
      @RequestParam("score") Double score,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    if (StringUtils.isBlank(appKey)) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }
    if (score == null) {
      return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
    }

    Order order = orderManager.findByIdAndAppKey(orderId, appKey);
    if (order == null) {
      return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_NOT_EXISTS_ERROR);
    }
    if (order.getStatus() != Order.ORDER_STATUS_OK) {
      return ResultTransport.getErrorBy(ErrorConstantV4.ORDER_STATUS_ERROR);
    }
    try {
      OrderReview orderReview = new OrderReview();
      orderReview.setContent(content);
      orderReview.setPassengerId(order.getPassengerId());
      orderReview.setPassengerName(order.getPassengerName());
      orderReview.setEmployId(order.getEmployId());
      orderReview.setEmployName(order.getEmployName());
      orderReview.setScore(score);
      orderReview.setOrderId(order.getId());
      orderReview.setOrderNo(order.getOrderNo());
      orderReview.setCreated(new Date());
      orderReview.setUpdated(new Date());
      orderReview.setAppKey(appKey);
      int create = orderReviewManager.insert(orderReview);
      if (create != 0) {
        order.setReview(true);
        order.setEmployScore(score);
        orderManager.update(order);

      } else {
        throw new RuntimeException("评价失败!");
      }

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(create);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;

  }

  /**
   * 根据订单id和appkey查询订单评价 .
   * @param orderId 订单id
   * @param appKey  区分系统
   */
  @ResponseBody
  @RequestMapping(value = "findReview", method = RequestMethod.GET)
  public ResultTransport findReviews(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    Preconditions.checkArgument(orderId != null && orderId != 0, "订单Id不能为空");
    Order order = orderManager.findByIdAndAppKey(orderId, appKey);
    Preconditions.checkArgument(order != null, "此订单不存在");
    try {

      OrderReview contents = orderReviewManager.findByOrderIdAndAppKey(orderId, appKey);

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(contents);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }

  
  /**
   * 根据司机id查询订单评价 .
   * @param driverId    司机id
   * @param page        页数(从1开始)
   * @param rows        每页数量
   * @param appKey      区分系统
   */
  @ResponseBody
  @RequestMapping(value = "findByDriver", method = RequestMethod.GET)
  public ResultTransport findByDriver(
      @RequestParam("driverId") Long driverId, 
      @RequestParam(required = false, value = "page") Integer page,
      @RequestParam(required = false, value = "rows") Integer rows, 
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {

      DataGrid<OrderReview> contents =
          orderReviewManager.findByDriver(driverId, page, rows, appKey);

      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(contents);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }

  /**
   * 删除订单评价 .
   * @param id      id
   * @param appKey  区分系统
   */
  @ResponseBody
  @RequestMapping(value = "deleteById", method = RequestMethod.DELETE)
  public ResultTransport delete(
      @RequestParam("id") Long id, 
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {

      orderReviewManager.deleteById(id, appKey);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }

  /**
   * 查询单个评价 .
   * @param id      id
   * @param appKey  区分系统
   */
  @ResponseBody
  @RequestMapping(value = "findOne", method = RequestMethod.GET)
  public ResultTransport findOne(
      @RequestParam("id") Long id, 
      @RequestParam("appKey") String appKey) {
    ResultTransport response = null;
    Preconditions.checkArgument(StringUtils.isNotBlank(appKey), "AppKey不能为空");
    try {

      OrderReview orderReview = orderReviewManager.findOne(id, appKey);
      response = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
      response.setData(orderReview);
    } catch (Exception ex) {
      ex.printStackTrace();
      response = ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
      return response;
    }
    return response;
  }
}
